class UsersController < ApplicationController
  before_filter :require_user, :only => [:show, :edit, :update]
  before_filter :require_admin, :only => [:index,:destroy]
  def new
    @user = User.new
  end

  def index
    @users = User.all
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @users }
    end
  end

  def create
    @user = User.new(params[:user])
    if @user.save
      flash[:notice] = "注册成功!"
      redirect_back_or_default root_url
    else
      render :action => :new, :layout => 'application'
    end
  end

  def show
    @user = @current_user
    @recent_comments = @user.comments
    #@title = "#{@user.username}的个人信息"
    respond_to do |format|
      format.html { render :layout => "application" }
      format.xml { render :xml => @user }
    end
  end

  def edit
    @user = @current_user
  end

  def update
    @user = @current_user
    if @user.update_attributes(params[:user])
      flash[:notice] = "帐号新更新成功！"
      redirect_to user_url
    else
      render :action => :edit
    end
  end

  private
  # => 清除右侧目录树片段的缓存
  def expire_fragment_cache
    expire_fragment(RecordSweeper::PATH.merge(:action_suffix => 'recent_visited_users'))
  end

end
